package com.baizhi.action;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.alibaba.fastjson.JSON;
import com.baizhi.Auction;
import com.baizhi.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Controller
public class UserAction {
    @Autowired
    private UserService us;
    private String name;
    private String pwd;
    private User u;
    private String yanzheng;
    private int pageIndex=1;
    private int pageSize;
    public void login(){
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            System.out.println(name);
            PrintWriter writer = response.getWriter();
            boolean login = us.login(name, pwd);
            String code = (String)ServletActionContext.getRequest().getSession().getAttribute("code");
            String contextPath = ServletActionContext.getRequest().getContextPath();
            //判断验证码是否一样  equalsIgnoreCase方法是不区分大小写的判断
            if (code.equalsIgnoreCase(yanzheng)&&login){
                writer.print("true");
            }else{
                writer.print("false");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void register(){
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            System.out.println(u);
            PrintWriter writer = response.getWriter();
            boolean register = us.register(u);
            String code = (String)ServletActionContext.getRequest().getSession().getAttribute("code");
            String contextPath = ServletActionContext.getRequest().getContextPath();
            //判断验证码是否一样  equalsIgnoreCase方法是不区分大小写的判断
            if (code.equalsIgnoreCase(yanzheng)&&register){
                writer.print("true");
            }else{
                writer.print("false");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void yazheng()  {
        //产生验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(116, 34, 4, 20);
        //把图片交给response的相应给浏览器
        HttpServletResponse response = ServletActionContext.getResponse();
        //获取到response的响应流
        ServletOutputStream stream = null;
        try {
            stream = response.getOutputStream();
            //将图片放入到response响应的流中
            captcha.write(stream);
            //获取到生成验证码的值
            String code = captcha.getCode();
            //放入session
            ServletActionContext.getRequest().getSession().setAttribute("code",code);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                stream.close();//关流;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void page(){
        PageInfo<Auction> page = us.page(pageSize, pageIndex);
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            response.setCharacterEncoding("utf8");
            PrintWriter writer = response.getWriter();
            String s = JSON.toJSONString(page);
            writer.print(s);
            System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public User getU() {
        return u;
    }

    public void setU(User u) {
        this.u = u;
    }

    public UserService getUs() {
        return us;
    }

    public void setUs(UserService us) {
        this.us = us;
    }

    public String getYanzheng() {
        return yanzheng;
    }

    public void setYanzheng(String yanzheng) {
        this.yanzheng = yanzheng;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}
